package com.wbk.lotterysystemuserservice.controller.admin.biz;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wbk.lotterysystemuserservice.entity.User;
import com.wbk.lotterysystemuserservice.entity.UserExt;
import com.wbk.lotterysystemuserservice.service.UserExtInfoService;
import com.wbk.lotterysystemuserservice.service.UserInfoService;
import com.wbk.lotterysystemuserservice.service.UserService;
import com.wbk.user.qo.UserBanQO;
import com.wbk.user.vo.UserExtVO;


import com.wbk.user.vo.UserVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import result.Result;

import java.util.List;

@Component
public class UserBiz {

    @Autowired
    UserService userService;

    @Autowired
    UserExtInfoService userExtInfoService;

    @Autowired
    UserInfoService userInfoService;

    public Result<PageInfo> userList(int pageNum, int pageSize, int navigatePages) {
        PageHelper.startPage(pageNum, pageSize);
        List<User> users = userService.getAllUser();
        PageInfo pageInfo = new PageInfo<>(users, navigatePages);
        return new Result<>(200, "用户列表", pageInfo);
    }

    public Result<UserExtVO> userDetail(Long userNo) {
        UserExt userExt = userExtInfoService.getUserExt(userNo);
        if (userExt == null) {
            return new Result<>(100, "找不到用户信息", null);
        }
        UserExtVO userExtVO = new UserExtVO();
        BeanUtils.copyProperties(userExt, userExtVO);
        return new Result<>(200, "用户信息", userExtVO);
    }

    public Result<String> userBan(UserBanQO userBanQO) {
        if (userBanQO.getExpire() < 1) {
            return new Result<>(100, "封禁失败", "封禁时间必须为整数且大于零");
        }
        User user = userInfoService.getUserByUserNo(userBanQO.getUserNo());
        if (user == null) {
            return new Result<>(100, "找不到用户信息", "找不到用户信息");
        }
        String result = userService.banUser(user.getUsername(), userBanQO);
        return new Result<>(200, "封禁成功", result);
    }

    public Result<UserVO> user(Long userNo) {
        User user = userInfoService.getUserByUserNo(userNo);
        if (user == null) {
            return new Result<>(100, "找不到用户", null);
        }
        UserVO userVO=new UserVO();
        BeanUtils.copyProperties(user, userVO);
        return new Result<>(200, "用户", userVO);
    }

    public Result<UserVO> userByMobile(String mobile) {
        User user = userInfoService.getUserByMobile(mobile);
        if (user == null) {
            return new Result<>(100, "找不到用户", null);
        }
        UserVO userVO=new UserVO();
        BeanUtils.copyProperties(user, userVO);
        return new Result<>(200, "用户", userVO);
    }
}
